package com.javaxiaobear.module.system.mapper;

import static com.javaxiaobear.module.system.domain.table.SysPostTableDef.SYS_POST;
import static com.javaxiaobear.module.system.domain.table.SysUserPostTableDef.SYS_USER_POST;
import static com.javaxiaobear.module.system.domain.table.SysUserTableDef.SYS_USER;

import com.javaxiaobear.base.common.utils.PageUtils;
import com.javaxiaobear.module.system.domain.SysPost;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import java.util.Arrays;
import java.util.List;

/**
 * 岗位信息 数据层
 *
 * @author javaxiaobear
 */
public interface SysPostMapper extends BaseMapper<SysPost> {
  /** 查询岗位数据集合 */
  default List<SysPost> selectPostList(SysPost post) {
    var query =
        QueryChain.of(this)
            .select(
                SYS_POST.POST_ID,
                SYS_POST.POST_CODE,
                SYS_POST.POST_NAME,
                SYS_POST.POST_SORT,
                SYS_POST.STATUS,
                SYS_POST.CREATE_BY,
                SYS_POST.CREATE_TIME,
                SYS_POST.REMARK)
            .where(SYS_POST.POST_ID.isNotNull())
            .and(
                SYS_POST
                    .POST_CODE
                    .like(post.getPostCode())
                    .when(post.getPostCode() != null && !post.getPostCode().isEmpty()))
            .and(
                SYS_POST
                    .STATUS
                    .eq(post.getStatus())
                    .when(post.getStatus() != null && !post.getStatus().isEmpty()))
            .and(
                SYS_POST
                    .POST_NAME
                    .like(post.getPostName())
                    .when(post.getPostName() != null && !post.getPostName().isEmpty()));
    // 处理排序
    if (post.getOrderByColumn() != null && !post.getOrderByColumn().isEmpty()) {
      if ("asc".equals(post.getIsAsc())) {
        query.orderBy(post.getOrderByColumn());
      } else {
        query.orderBy(post.getOrderByColumn(), false);
      }
    }

    return query.list();
  }

  default Page<SysPost> selectPostPage(SysPost post) {
    var query =
        QueryChain.of(this)
            .select(
                SYS_POST.POST_ID,
                SYS_POST.POST_CODE,
                SYS_POST.POST_NAME,
                SYS_POST.POST_SORT,
                SYS_POST.STATUS,
                SYS_POST.CREATE_BY,
                SYS_POST.CREATE_TIME,
                SYS_POST.REMARK)
            .where(SYS_POST.POST_ID.isNotNull())
            .and(
                SYS_POST
                    .POST_CODE
                    .like(post.getPostCode())
                    .when(post.getPostCode() != null && !post.getPostCode().isEmpty()))
            .and(
                SYS_POST
                    .STATUS
                    .eq(post.getStatus())
                    .when(post.getStatus() != null && !post.getStatus().isEmpty()))
            .and(
                SYS_POST
                    .POST_NAME
                    .like(post.getPostName())
                    .when(post.getPostName() != null && !post.getPostName().isEmpty()));
    // 处理排序
    if (post.getOrderByColumn() != null && !post.getOrderByColumn().isEmpty()) {
      if ("asc".equals(post.getIsAsc())) {
        query.orderBy(post.getOrderByColumn());
      } else {
        query.orderBy(post.getOrderByColumn(), false);
      }
    }

    return query.page(PageUtils.getPage());
  }

  /** 查询所有岗位 */
  default List<SysPost> selectPostAll() {
    return QueryChain.of((BaseMapper<SysPost>) this)
        .select(
            SYS_POST.POST_ID,
            SYS_POST.POST_CODE,
            SYS_POST.POST_NAME,
            SYS_POST.POST_SORT,
            SYS_POST.STATUS,
            SYS_POST.CREATE_BY,
            SYS_POST.CREATE_TIME,
            SYS_POST.REMARK)
        .list();
  }

  /** 通过岗位ID查询岗位信息 */
  default SysPost selectPostById(Long postId) {
    return QueryChain.of((BaseMapper<SysPost>) this)
        .select(
            SYS_POST.POST_ID,
            SYS_POST.POST_CODE,
            SYS_POST.POST_NAME,
            SYS_POST.POST_SORT,
            SYS_POST.STATUS,
            SYS_POST.CREATE_BY,
            SYS_POST.CREATE_TIME,
            SYS_POST.REMARK)
        .where(SYS_POST.POST_ID.eq(postId))
        .one();
  }

  /** 根据用户ID获取岗位选择框列表 */
  default List<Long> selectPostListByUserId(Long userId) {
    return QueryChain.of((BaseMapper<SysPost>) this)
        .select(SYS_POST.POST_ID)
        .from(SYS_POST)
        .leftJoin(SYS_USER_POST)
        .on(SYS_USER_POST.POST_ID.eq(SYS_POST.POST_ID))
        .leftJoin(SYS_USER)
        .on(SYS_USER.USER_ID.eq(SYS_USER_POST.USER_ID))
        .where(SYS_USER.USER_ID.eq(userId))
        .listAs(Long.class);
  }

  /** 查询用户所属岗位组 */
  default List<SysPost> selectPostsByUserName(String userName) {
    return QueryChain.of((BaseMapper<SysPost>) this)
        .select(SYS_POST.POST_ID, SYS_POST.POST_NAME, SYS_POST.POST_CODE)
        .from(SYS_POST)
        .leftJoin(SYS_USER_POST)
        .on(SYS_USER_POST.POST_ID.eq(SYS_POST.POST_ID))
        .leftJoin(SYS_USER)
        .on(SYS_USER.USER_ID.eq(SYS_USER_POST.USER_ID))
        .where(SYS_USER.USER_NAME.eq(userName))
        .list();
  }

  /** 删除岗位信息 */
  default int deletePostById(Long postId) {
    return deleteById(postId);
  }

  /** 批量删除岗位信息 */
  default int deletePostByIds(Long[] postIds) {
    if (postIds == null || postIds.length == 0) {
      return 0;
    }
    return deleteBatchByIds(Arrays.asList(postIds));
  }

  /** 修改岗位信息 */
  default int updatePost(SysPost post) {
    return update(post);
  }

  /** 新增岗位信息 */
  default int insertPost(SysPost post) {
    return insert(post);
  }

  /** 校验岗位名称 */
  default SysPost checkPostNameUnique(String postName) {
    return QueryChain.of((BaseMapper<SysPost>) this)
        .select(
            SYS_POST.POST_ID,
            SYS_POST.POST_CODE,
            SYS_POST.POST_NAME,
            SYS_POST.POST_SORT,
            SYS_POST.STATUS,
            SYS_POST.CREATE_BY,
            SYS_POST.CREATE_TIME,
            SYS_POST.REMARK)
        .where(SYS_POST.POST_NAME.eq(postName))
        .limit(1)
        .one();
  }

  /** 校验岗位编码 */
  default SysPost checkPostCodeUnique(String postCode) {
    return QueryChain.of((BaseMapper<SysPost>) this)
        .select(
            SYS_POST.POST_ID,
            SYS_POST.POST_CODE,
            SYS_POST.POST_NAME,
            SYS_POST.POST_SORT,
            SYS_POST.STATUS,
            SYS_POST.CREATE_BY,
            SYS_POST.CREATE_TIME,
            SYS_POST.REMARK)
        .where(SYS_POST.POST_CODE.eq(postCode))
        .limit(1)
        .one();
  }
}
